﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;

public partial class profile : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    //公司介紹NodeId=3
    private int m_NodeId = 3;
    private string m_MainPath = "profile.aspx";

    private int m_Mode
    {
        get { if (ViewState["mode"] == null) { ViewState["mode"] = 0; } return int.Parse(ViewState["mode"].ToString()); }
        set { ViewState["mode"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();

        if (!IsPostBack)
        {
            m_Mode = QueryStringHelper.GetInteger("id", 0);

            //清單
            Repeater_Bind();

            //內容
            if (m_Mode != 0)
            {
                LoadDataToUI();
            }
        }
    }

    private void LoadDataToUI()
    {
        PostVO postVO = m_PostService.GetPostById(m_Mode);
        lblTitle.Text = postVO.Title;
        ltlHtmlContent.Text = postVO.HtmlContent;
    }

    private void Repeater_Bind()
    {
        string sortField = "SortNo";
        bool sortDesc = false;

        IList<PostVO> postList = m_PostService.GetPostListByNodeId(m_NodeId, false, sortField, sortDesc);
        if (postList != null && postList.Count > 0)
        {
            if (m_Mode == 0)
            {
                Response.Redirect(string.Format("{0}?id={1}", m_MainPath, postList[0].PostId));
            }
        }

        Repeater1.DataSource = m_PostService.GetPostListByNodeId(m_NodeId, false, sortField, sortDesc);
        Repeater1.DataBind();
    }
}